package com.blog.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.blog.common.thread.BaseThread;
import com.blog.entity.dto.UserLoginDTO;
import com.blog.entity.dto.UserPwdDTO;
import com.blog.entity.po.User;
import com.blog.mapper.UserMapper;
import com.blog.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User login(UserLoginDTO dto) {
        User user = lambdaQuery()
                .eq(User::getUsername, dto.getUsername()).one();
        if (ObjUtil.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }

        // 对密码进行md5 加密再进行校验
        String md5toPwd = DigestUtils.md5Hex(dto.getPassword());
        if (!Objects.equals(user.getPassword(), md5toPwd)) {
            throw new RuntimeException("密码错误");
        }

        return user;
    }

    @Override
    public void register(UserLoginDTO dto) {
        // 判断用户是否存在
        User user = lambdaQuery().eq(User::getUsername, dto.getUsername()).one();
        if (!ObjUtil.isEmpty(user)) {
            throw new RuntimeException("用户已存在");
        }

        // 密码加密 和 补充基本信息
        User saveUser = new User();
        long id = IdUtil.getSnowflake().nextId();
        saveUser.setId(id + "");
        saveUser.setUsername(dto.getUsername());
        saveUser.setPassword(DigestUtils.md5Hex(dto.getPassword()));
        saveUser.setStatus(1);
        if (ObjUtil.isEmpty(dto.getAvatar())) {
            saveUser.setAvatar("http://localhost:8081//attach/2023/04/23/dd346aba-e20a-4592-b437-b3a845206560.jpeg");
        }
        boolean b = save(saveUser);
        if (!b) {
            throw new RuntimeException("注册失败");
        }
    }

    @Override
    public void updateInfo(User user) {
        boolean b = lambdaUpdate()
                .eq(User::getId, user.getId())
                .set(User::getAvatar, user.getAvatar())
                .set(User::getNickname, user.getNickname())
                .set(User::getSex, user.getSex())
                .set(User::getBirthday, user.getBirthday())
                .set(User::getIntro, user.getIntro())
                .set(User::getUpdateTime, LocalDateTime.now())
                .update();
        if (!b) {
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    public void updatePwd(UserPwdDTO dto) {
        if (!Objects.equals(dto.getRePwd(), dto.getNewPwd())) {
            throw new RuntimeException("两次密码不一致");
        }
        String id = BaseThread.getId();
        User user = lambdaQuery().eq(User::getId, id).one();
        if (ObjUtil.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }

        String md5OldPws = DigestUtils.md5Hex(dto.getOldPwd());
        if (!user.getPassword().equals(md5OldPws)) {
            throw new RuntimeException("原密码错误");
        }
        user.setPassword(DigestUtils.md5Hex(dto.getNewPwd()));
        boolean b = lambdaUpdate()
                .eq(User::getId, user.getId())
                .set(User::getPassword, user.getPassword())
                .set(User::getUpdateTime, LocalDateTime.now())
                .update();
        if (!b) {
            throw new RuntimeException("更新失败");
        }


    }


}
